<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ES6中的Set结构</title>
</head>

<body>

</body>

<script>
    const setA = new Set([1, 2, 3, 4])
    const setB = new Set([3, 4, 5, 6])
    // 求并集
    function union(setA, setB) {
        return new Set([...setA, ...setB])
    }
    // 求交集
    function intersection(setA, setB) {
        const resultSet = new Set()
        for (let element of setA) {
            if (setB.has(element)) {
                resultSet.add(element)
            }
        }
        return resultSet
    }
    // 求差集
    function difference(setA, setB) {
        const resultSet = new Set(setA)
        for (let element of setB) {
            if (!setA.has(element)) {
                resultSet.add(element)
            } else {
                resultSet.delete(element)
            }
        }
        return resultSet
    }
    console.log('并集为：', union(setA, setB))
    console.log('交集为：', intersection(setA, setB))
    console.log('差集为：', difference(setA, setB))
</script>

</html>